package org.example.everyday;

import java.security.cert.PolicyNode;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * Description:TODO 详细描述
 * <p>
 * Copyright: 天津联想协同科技有限公司
 * <p>
 * https://leetcode-cn.com/problems/detect-squares/
 * @author anlu
 * @date 2022/1/26 18:50
 */
public class DetectSquares {

    Map<Integer,Map<Integer,Integer>> row2Col = new HashMap<>();


    public void add(int[] point){
        int x = point[0],y = point[1];
        Map<Integer,Integer> colCnt = row2Col.getOrDefault(x,new HashMap<>());
        colCnt.put(y,colCnt.getOrDefault(y,0)+1);
        row2Col.put(x,colCnt);
    }

    public int count(int[] point){
        int x = point[0],y = point[1];
        int ans = 0;
        Map<Integer,Integer> col2Cnt = row2Col.getOrDefault(x,new HashMap<>());
        for (int ny: col2Cnt.keySet()){
            if(ny == y){
                continue;
            }
            int c1 = col2Cnt.get(ny);
            int len = y - ny;
            int[] nums = new int[]{x+len,x-len};
            for (int nx:nums){
                Map<Integer,Integer> temp =  row2Col.getOrDefault(nx,new HashMap<>());
                int c2 = temp.getOrDefault(y,0),c3 = temp.getOrDefault(ny,0);
                ans+=c1*c2*c3;
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        DetectSquares detectSquares = new DetectSquares();
        detectSquares.add(new int[]{3, 10});
        detectSquares.add(new int[]{11, 2});
        detectSquares.add(new int[]{3, 2});
        int result = detectSquares.count(new int[]{11, 10});
        System.out.println(result);


    }




}
